package com.css.dzj.dzjap.modules.export.service;

import com.css.dzj.dzjap.modules.expertcredit.dao.ExpertCreditDao;
import com.css.dzj.dzjap.modules.expertcredit.entity.ApExpertCreditEntity;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ImportServiceImpl implements ImportService {

    @Autowired
    private ExpertCreditDao expertCreditDao;
   //专家导入
    @Override
    public String expertImport(MultipartFile file) {
        try{
            if(file == null || file.isEmpty()){
                throw new RuntimeException("导入文件不能为空");
            }
            //获取文件名
           String fileName = file.getOriginalFilename();
            int len = fileName.lastIndexOf(".");
            if(len <=0){
                throw  new RuntimeException("文件类型有误，请确认！");
            }
            String fileType = fileName.substring(len + 1).toLowerCase();

            if(!fileType.equals("xls") && !fileType.equals("xlsx")){
                throw new RuntimeException("只允许上传xls、xlsx");
            }
            InputStream in =file.getInputStream();
            Workbook wb = ExcelUtil.chooseWorkbook(fileName,in);
            List<ApExpertCreditEntity> expertCredit = getCompaniesFromExcel(wb);
         /*   List<String> creditCodes = expertCredit.stream().map(ApExpertCreditEntity::getExpertCode).collect(Collectors.toList());
            List<ApExpertCreditEntity> exists = expertCreditDao*/
           for(int i =0;i<expertCredit.size();i++){
               ApExpertCreditEntity eCredit = expertCredit.get(i);
               expertCreditDao.save(eCredit);
           }
            return  "企业导入成功！";
        }catch (Exception e){
            return "0";
        }

    }

    private Integer getRowNum(Sheet sheet){
        int totalRows = sheet.getPhysicalNumberOfRows();
        for(int r = 1;r < totalRows;r++){
            Row row = sheet.getRow(r);
            if(row == null){
                return r;
            }
           String companyName = ExcelUtil.getCellValue(row.getCell(0));
           String creditCode = ExcelUtil.getCellValue(row.getCell(1));
           if(companyName.trim().equals("") && creditCode.trim().equals("")){
               return  r;
           }
        }
        return totalRows;
    }

    private List<ApExpertCreditEntity> getCompaniesFromExcel(Workbook wb){
        try{
            List<ApExpertCreditEntity> eCredit = new ArrayList<>();
           /* *
             * 得到第一个shell
             */
            Sheet sheet = wb.getSheetAt(0);
           /* *
             * 得到Excel的行数*/

            int totalRows = getRowNum(sheet);
            List<String> creditCodes = new ArrayList<>();
            FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

           /* *
             * 循环Excel的行*/

            for(int r=1;r<totalRows;r++){
                ApExpertCreditEntity apExpertCreditEntity = new ApExpertCreditEntity();

                Row row = sheet.getRow(r);
                if(row ==null){
                    continue;
                }
            String cellValue = ExcelUtil.getCellValue(row.getCell(0));
                if(cellValue.trim().equals("")){
                    throw new RuntimeException("专家评分编号不能为空,请确认！");
                }
                apExpertCreditEntity.setExpertCode(cellValue);

                 cellValue = ExcelUtil.getCellValue(row.getCell(1));
                if(cellValue.trim().equals("")){
                    throw new RuntimeException("专家姓名不能为空，请确认！");
                }
                apExpertCreditEntity.setExpertName(cellValue);

                cellValue = ExcelUtil.getCellValue(row.getCell(2));
                if(cellValue.trim().equals("")){
                    throw new RuntimeException("证件号码不能为空，请确认！");
                }
                apExpertCreditEntity.setExpertIdcard(cellValue);

                cellValue = ExcelUtil.getCellValue(row.getCell(3));
                if(cellValue.trim().equals("")){
                    throw new RuntimeException("证件类型不能为空，请确认！");
                }
                apExpertCreditEntity.setCertype(cellValue);

                cellValue = ExcelUtil.getCellValue(row.getCell(4));
                if(cellValue.trim().equals("")){
                    throw new RuntimeException("专家信用分数不能为空，请确认！");
                }
                apExpertCreditEntity.setExpertScore(cellValue);

                cellValue = ExcelUtil.getCellValue(row.getCell(5));
                if(cellValue.trim().equals("")){
                    throw new RuntimeException("专家分类不能为空，请确认！");
                }
                apExpertCreditEntity.setExpertClassification(cellValue);

                cellValue = ExcelUtil.getCellValue(row.getCell(6));
                if(cellValue.trim().equals("")){
                    throw new RuntimeException("专家等级不能为空，请确认！");
                }
                apExpertCreditEntity.setExpertLevl(cellValue);
                eCredit.add(apExpertCreditEntity);
            }
            return  eCredit;
        }catch (Exception e){

            return  null;
        }

    }


}
